package com.example.lot_lock;

import android.app.Application;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ApplicationTCP extends Application {
    private Socket socket;
    private OutputStream out = null;
    private InputStream in = null;

    public String userNmae = null;//用户名字
    private String id1 = null;//设备序列号
    private String id2 = null;
    private String id3 = null;
    private boolean equipState1 = false;//状态
    private boolean equipState2 = false;
    private boolean equipState3 = false;

    public void Init() throws IOException {
        this.socket = new Socket("39.103.138.93", 75);
        this.out = socket.getOutputStream();
        this.in = socket.getInputStream();
    }
    public OutputStream getOut(){
        return out;
    }
    public  InputStream getIn(){
        return in;
    }
    void closeSocket() throws IOException {
        out.close();
        in.close();
        socket.close();
    }


    public void updateEquipState() throws InterruptedException {//根据用户名在线更新设备信息
        updateEquipState update = new updateEquipState();
        update.start();
        update.join();

        ReadThread readThread = new ReadThread();
        readThread.start();
        readThread.join();
    }

    class  ReadThread extends Thread{//接收线程
        byte []receiveData = new byte[15];
        @Override
        public void run() {
            super.run();
            try {
                in.read(receiveData,0,15);
                id1 = new String(""+(char)receiveData[0]+ (char)receiveData[1]+(char)receiveData[2]+(char)receiveData[3]);
                id2 = new String(""+(char)receiveData[4] + (char)receiveData[5]+(char)receiveData[6]+(char)receiveData[7]);
                id3 = new String(""+(char)receiveData[8] + (char)receiveData[9]+(char)receiveData[10]+(char)receiveData[11]);
                equipState1= receiveData[12] == '1'? true:false;
                equipState2= receiveData[13] == '1'? true:false;
                equipState3= receiveData[14] == '1'? true:false;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    class updateEquipState extends Thread {
       String sendData = '3'+ userNmae + '\n';
        @Override
        public void run() {
            super.run();
            try {
                out.write(sendData.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    public  String getId1(){
        return this.id1;
    }
    public  String getId2(){
        return this.id2;
    }
    public  String getId3(){
        return this.id3;
    }
    public boolean equipState1(){
        return this.equipState1;
    }
    public boolean equipState2(){
        return this.equipState2;
    }
    public boolean equipState3(){
        return this.equipState3;
    }
}
